package main

import (
	"fmt"
	"os"
	"os/signal"
	"reward-server/global"
	"reward-server/initialize"
	myvalidator "reward-server/validator"
	"syscall"

	"github.com/gin-gonic/gin/binding"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

// https://sepolia.etherscan.io/address/0x93B8ECF277Cd26a603B12c50Fd4920484bd88Ff2#code
// https://sepolia.etherscan.io/address/0x373daA68f1D1f3696fa27ed5dE7072c18E3eC54e#code
func main() {

	//1初始化日志
	initialize.InitLog()

	//1. 初始化配置文件
	initialize.InitConfig()

	//2. 初始化routers
	Router := initialize.Routers()

	//3. 初始化翻译
	if err := initialize.InitTrans("zh"); err != nil {
		panic(err)
	}
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		_ = v.RegisterValidation("mobile", myvalidator.ValidateMobile)
		_ = v.RegisterTranslation("mobile", global.Tsrans, func(ut ut.Translator) error {
			return ut.Add("mobile", "{0} 非法的手机号码!", true) // see universal-translator for details
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("mobile", fe.Field())
			return t
		})
	}
	//4.初始化数据库
	initialize.InitDb()

	//5.初始化redis
	initialize.NewRedis()

	//6 初始化区块链链接
	initialize.InitEth()
	//7 启动服务
	go func() {

		//启动gin 路由
		if err := Router.Run(fmt.Sprintf(":%d", global.ServerConfig.Port)); err != nil {
			zap.S().Errorf("启动失败:%v", err)
		}
		zap.S().Infof("端口号:%v,启动成功", global.ServerConfig.Port)

	}()

	//9 初始化延迟队列
	// go func() {
	// 	initialize.DelayQueueInit()
	// }()
	zap.S().Info("payServer Starting server at")
	//接收终止信号
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

	<-quit

}
